package com.nolon.flange.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.nolon.flange.R
import com.nolon.flange.databinding.ItemOrderBinding
import com.nolon.flange.databinding.ItemOrderHeaderBinding
import com.nolon.flange.model.Order

class OrderAdapter(val acc: AppCompatActivity, private val onItemClick: (Order) -> Unit) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var tableHeaderColor: Int = ContextCompat.getColor(acc, R.color.table_1_header)
    var tableRowOddColor: Int =  ContextCompat.getColor(acc,R.color.table_1_row_1)
    var tableRowEvenColor: Int =  ContextCompat.getColor(acc,R.color.table_1_row_2)

    var orderList = mutableListOf<Order>()
    fun swapData(orders: List<Order>) {
        orderList.clear()
        orderList.addAll(orders)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == HEADER_TYPE) {
            val binding =
                ItemOrderHeaderBinding.inflate(LayoutInflater.from(parent.context), parent, false)
            OrderHeaderViewHolder(binding)
        } else {
            val binding =
                ItemOrderBinding.inflate(LayoutInflater.from(parent.context), parent, false)
            OrderViewHolder(binding)
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (position == 0 && holder is OrderHeaderViewHolder) {
            holder.binding.layout.setBackgroundColor(tableHeaderColor)
        }
        if (position > 0 && holder is OrderViewHolder) {
            val order = orderList[position - 1]
            val flange = order.flange
            with(holder.binding) {
                textNumber.text = position.toString()
                textMaterialSpec.text = flange.materialSpec.toString()
                textFlangeSpec.text = "${flange.length} × ${flange.width}"
                textCount.text = order.requiredNumber.toString()
                textHasHole.text = if (order.flange.xBigHoleCount != 0) "是" else "否"
                layout.apply {
                    setOnClickListener { onItemClick(order) }
                    if (position.rem(2) == 0) {
                        setBackgroundColor(tableRowEvenColor)
                    } else {
                        setBackgroundColor(tableRowOddColor)
                    }
                }
            }
        }
    }

    override fun getItemViewType(position: Int) = if (position == 0) HEADER_TYPE else CONTENT_TYPE


    override fun getItemCount(): Int {
        return orderList.size + 1
    }

    class OrderViewHolder(val binding: ItemOrderBinding) : RecyclerView.ViewHolder(binding.root)

    class OrderHeaderViewHolder(val binding: ItemOrderHeaderBinding) :
        RecyclerView.ViewHolder(binding.root)

    companion object {
        const val HEADER_TYPE = 0
        const val CONTENT_TYPE = 1
    }
}